package rootPackage.controller.creazione.dbProdotti;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.RequestMap;

import rootPackage.meta.ProdottiMeta;
import rootPackage.service.ProdottiService;




public class AggiornaController extends Controller {

    private ProdottiService service = new ProdottiService();

    private ProdottiMeta meta = ProdottiMeta.get();

    @Override
    public Navigation run() throws Exception {
        if (!validate()) {
            return forward("edit.jsp");
        }
        service.update(asKey(meta.key), asLong(meta.version), new RequestMap(
            request));
        return redirect(basePath);
    }

    protected boolean validate() {
        Validators v = new Validators(request);
        v.add(meta.nome, v.required());
        v.add(meta.descrizione, v.required());
        return v.validate();
    }
}